/* Copyright (c) 2011-2013 ISP RAS, Moscow. For more information please write to
 
      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ru.ispras.coffeemachine.util;


/**
 * Executor output
 * 
 * @author Sergey Vartanov <me@enzet.ru>
 * @version 1.2.0
 * @since 2011-10-25
 */
public class Output
{
	String out;
	String err;

	public Output(String out, String err)
	{
		this.out = out;
		this.err = err;
	}

	public String getOut()
	{
		return out;
	}

	public String getErr()
	{
		return err;
	}

	/**
	 * Prints contents of execution error output if there are any.
	 */
	public void checkErrors()
	{
		if (!err.equals(""))
		{
			Logger.error(err);
		}
	}
	
	
	/**
	 * Filters output contents using a line prefix.
	 * @param linePrefix line prefix
	 * @return filtered output
	 */
	public String getFilteredOutput(String linePrefix)
	{
		StringBuilder result = new StringBuilder();
		String newLine = System.getProperty("line.separator");
		String []lines = out.split(newLine);
		for (String line : lines) {
			if (line.startsWith(linePrefix)) {
				result.append(line.substring(linePrefix.length())).append(newLine);
			}
		}
		
		return result.toString();
	}
}
